<?php
class Chartm extends CI_Model {

	//构造函数
	public function __construct()
	{
		parent::__construct();
	}

	public function index(){
	//一级代理
	$sql = "select count(*) agent1 from mifi_agent where agent_level = 1";
        $agent1 = $this->db->query($sql)->row_array();

        //二级代理
		$sql = "select count(*) agent2 from mifi_agent where agent_level = 2";
        $agent2 = $this->db->query($sql)->row_array();
        
        //总代理
	    $sql = "select count(*) agents from mifi_agent";
        $agents = $this->db->query($sql)->row_array();

        //代理数量
        $agent = array_merge($agent1,$agent2,$agents);

        //会员数量
        $sql = "select count(*) users from mifi_users";
        $users = $this->db->query($sql)->row_array();

        //订单总数
        $sql = "select count(*) orders from mifi_order";
        $orders = $this->db->query($sql)->row_array();

        //今日订单
        $sql = "select count(*) time from mifi_order where to_days(starttime) = to_days(now())";
        $time = $this->db->query($sql)->row_array();
        
        //昨日订单
        $sql = "select count(*) time1 from mifi_order where to_days(now()) - to_days(starttime) = 1";
        $time1 = $this->db->query($sql)->row_array();
        
        //前日订单
        $sql = "select count(*) time2 from mifi_order where to_days(now()) - to_days(starttime) = 2";
        $time2 = $this->db->query($sql)->row_array();

        $sql = "select count(*) time3 from mifi_order where to_days(now()) - to_days(starttime) = 3";
        $time3 = $this->db->query($sql)->row_array(); 

        $sql = "select count(*) time4 from mifi_order where to_days(now()) - to_days(starttime) = 4";
        $time4 = $this->db->query($sql)->row_array(); 

        $sql = "select count(*) time5 from mifi_order where to_days(now()) - to_days(starttime) = 5";
        $time5 = $this->db->query($sql)->row_array();  

        $sql = "select count(*) time6 from mifi_order where to_days(now()) - to_days(starttime) = 6";
        $time6 = $this->db->query($sql)->row_array();                 
        //订单数量
        $order = array_merge($time,$time1,$time2,$time3,$time4,$time5,$time6,$orders);

        //设备总数
        $sql = "select count(*) devices from mifi_device";
        $devices = $this->db->query($sql)->row_array();

        //今日设备
        $sql = "select count(*) times from mifi_device where to_days(addtime) = to_days(now())";
        $times = $this->db->query($sql)->row_array();
        
        //昨日设备
        $sql = "select count(*) times1 from mifi_device where to_days(now()) - to_days(addtime) = 1";
        $times1 = $this->db->query($sql)->row_array();
        
        //前日设备
        $sql = "select count(*) times2 from mifi_device where to_days(now()) - to_days(addtime) = 2";
        $times2 = $this->db->query($sql)->row_array();

        $sql = "select count(*) times3 from mifi_device where to_days(now()) - to_days(addtime) = 3";
        $times3 = $this->db->query($sql)->row_array(); 

        $sql = "select count(*) times4 from mifi_device where to_days(now()) - to_days(addtime) = 4";
        $times4 = $this->db->query($sql)->row_array(); 

        $sql = "select count(*) times5 from mifi_device where to_days(now()) - to_days(addtime) = 5";
        $times5 = $this->db->query($sql)->row_array();  

        $sql = "select count(*) times6 from mifi_device where to_days(now()) - to_days(addtime) = 6";
        $times6 = $this->db->query($sql)->row_array();

        //设备数量
        $device = array_merge($times,$times1,$times2,$times3,$times4,$times5,$times6,$devices);

        //今日订单金额
        $sql = "select sum(order_money) as moneys from mifi_order where to_days(starttime) = to_days(now())";
        $omoneys = $this->db->query($sql)->row_array();
        if(is_null($omoneys['moneys'])){
        	$omoneys['moneys'] = 0;
        }
        $otime['times'] = date("Y-m-d");

        //昨日订单金额
        $sql = "select sum(order_money) as money1 from mifi_order where to_days(now()) - to_days(starttime) = 1";
        $omoney1 = $this->db->query($sql)->row_array();
        if(is_null($omoney1['money1'])){
        	$omoney1['money1'] = 0;
        }
        $otime['time1'] = date("Y-m-d",strtotime("-1 day"));
        
        //前日订单金额
        $sql = "select sum(order_money) as money2 from mifi_order where to_days(now()) - to_days(starttime) = 2";
        $omoney2 = $this->db->query($sql)->row_array();
        if(is_null($omoney2['money2'])){
        	$omoney2['money2'] = 0;
        }
        $otime['time2'] = date("Y-m-d",strtotime("-2 day"));

        $sql = "select sum(order_money) as money3 from mifi_order where to_days(now()) - to_days(starttime) = 3";
        $omoney3 = $this->db->query($sql)->row_array();
        if(is_null($omoney3['money3'])){
        	$omoney3['money3'] = 0;
        }
        $otime['time3'] = date("Y-m-d",strtotime("-3 day"));

        $sql = "select sum(order_money) as money4 from mifi_order where to_days(now()) - to_days(starttime) = 4";
        $omoney4 = $this->db->query($sql)->row_array();
        if(is_null($omoney4['money4'])){
        	$omoney4['money4'] = 0;
        }
        $otime['time4'] = date("Y-m-d",strtotime("-4 day"));

        $sql = "select sum(order_money) as money5 from mifi_order where to_days(now()) - to_days(starttime) = 5";
        $omoney5 = $this->db->query($sql)->row_array();
        if(is_null($omoney5['money5'])){
        	$omoney5['money5'] = 0;
        }
        $otime['time5'] = date("Y-m-d",strtotime("-5 day"));

        $sql = "select sum(order_money) as money6 from mifi_order where to_days(now()) - to_days(starttime) = 6";
        $omoney6 = $this->db->query($sql)->row_array();
        if(is_null($omoney6['money6'])){
        	$omoney6['money6'] = 0;
        }
        $otime['time6'] = date("Y-m-d",strtotime("-6 day"));

        $sql = "select sum(order_money) as money7 from mifi_order where to_days(now()) - to_days(starttime) = 7";
        $omoney7 = $this->db->query($sql)->row_array();
        if(is_null($omoney7['money7'])){
        	$omoney7['money7'] = 0;
        }
        $otime['time7'] = date("Y-m-d",strtotime("-7 day"));

        $sql = "select sum(order_money) as money8 from mifi_order where to_days(now()) - to_days(starttime) = 8";
        $omoney8 = $this->db->query($sql)->row_array();
        if(is_null($omoney8['money8'])){
        	$omoney8['money8'] = 0;
        }
        $otime['time8'] = date("Y-m-d",strtotime("-8 day"));
        
        //每日订单金额
        $omoney = array_merge($omoney1,$omoney2,$omoney3,$omoney4,$omoney5,$omoney6,$omoney7,$omoney8,$omoneys);

        //在线人数
        $sql = "select num as nums from mifi_people where to_days(time) = to_days(now())";
        $nums = $this->db->query($sql)->row_array();
        if(!$nums){
        	$nums['nums'] = 0;
        }
  
        //昨日在线人数
        $sql = "select num as num1 from mifi_people where to_days(now()) - to_days(time) = 1";
        $num1 = $this->db->query($sql)->row_array();
        if(!$num1){
        	$num1['num1'] = 0;
        }

        //前日在线人数
        $sql = "select num as num2 from mifi_people where to_days(now()) - to_days(time) = 2";
        $num2 = $this->db->query($sql)->row_array();
        if(!$num2){
        	$num2['num2'] = 0;
        }

        $sql = "select num as num3 from mifi_people where to_days(now()) - to_days(time) = 3";
        $num3 = $this->db->query($sql)->row_array();
        if(!$num3){
        	$num3['num3'] = 0;
        }

        $sql = "select num as num4 from mifi_people where to_days(now()) - to_days(time) = 4";
        $num4 = $this->db->query($sql)->row_array();
        if(!$num4){
        	$num4['num4'] = 0;
        }

        $sql = "select num as num5 from mifi_people where to_days(now()) - to_days(time) = 5";
        $num5 = $this->db->query($sql)->row_array();
        if(!$num5){
        	$num5['num5'] = 0;
        }

        $sql = "select num as num6 from mifi_people where to_days(now()) - to_days(time) = 6";
        $num6 = $this->db->query($sql)->row_array();
        if(!$num6){
        	$num6['num6'] = 0;
        }
        
        //每日在线人数统计
        $people = array_merge($num1,$num2,$num3,$num4,$num5,$num6,$nums);
	echo json_encode(['code'=>1,'infomsg'=>'获取成功','agent'=>$agent,'users'=>$users,'order'=>$order,'device'=>$device,'otime'=>$otime,'omoney'=>$omoney,'people'=>$people]);
	}
}